﻿namespace HelenServer.Grpc.Contracts;

public static class GrpcResultExtensions
{
    public static GrpcResult ToGrpcResult(this OperationResult result)
    {
        return new(
            result.Status,
            result.Message,
            result.FailedHistory);
    }

    public static GrpcResult<GData> ToGrpcResult<GData>(this OperationResult result)
    {
        return new(
            result.Status,
            result.Message,
            result.FailedHistory,
            default!);
    }

    public static GrpcResult<GData> ToGrpcResult<OData, GData>(
        this OperationResult<OData> result, Func<OData, GData>? func = null)
    {
        return new(
            result.Status,
            result.Message,
            result.FailedHistory,
            result.Succeed && func is not null ? func(result.Data) : default!);
    }

    public static OperationResult ToOperationResult(this GrpcResult result)
    {
        return new OperationResult(
            result.Status,
            result.Message,
            result.FailedHistory);
    }

    public static OperationResult ToOperationResult<GData>(this GrpcResult<GData> result)
    {
        return new OperationResult(
            result.Status,
            result.Message,
            result.FailedHistory);
    }

    public static OperationResult<OData> ToOperationResult<OData, GData>(
        this GrpcResult<GData> result, Func<GData, OData> func)
    {
        return new OperationResult<OData>(
            result.Status,
            result.Message,
            result.FailedHistory,
            func(result.Data));
    }
}